Stream API are designed to be efficient and can support improving the performance of programs. We can avoid unnecessary loops and iterations in our programs by using Stream functionality. Streams can be used for filtering, collecting, printing, and converting data from one structure to another such as one collection to another collection.
Stream stream = collection.strim();
import java.io.*; import java.util.*; import java.util.stream.*; public class StreamFilterExample { static void main (
String
args[]){ int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //Filtering the even numbers from a collection List<
Integer
> evenList = Arrays.asList(numbers).stream() .filter(i -> i % 2 == 0) .collect(Collectors.toList()); System.out.println( "Even numbers after stream operation : "+ evenList ); } } Output: Even numbers after stream operation : [2, 4, 6, 8, 10]